/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.inventory.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.base.dao.StoreProductDao;
import com.chlitina.store.modules.base.entity.StoreProduct;
import com.chlitina.store.modules.cash.dao.StoreConsumeBuyProductDao;
import com.chlitina.store.modules.cash.entity.StoreConsume;
import com.chlitina.store.modules.cash.entity.StoreConsumeBuyProduct;
import com.chlitina.store.modules.inventory.dao.StoreInventoryAlertDao;
import com.chlitina.store.modules.inventory.entity.StoreInventoryAlert;
import com.chlitina.store.modules.sys.entity.Store;
import com.chlitina.store.modules.sys.utils.StoreUtils;
import com.google.common.collect.Lists;

/**
 * 库存盘点预警Service
 * @author lvbo
 * @version 2013-11-22
 */
@Component
@Transactional(readOnly = true)
public class StoreInventoryAlertService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(StoreInventoryAlertService.class);
	
	@Autowired
	private StoreInventoryAlertDao storeInventoryAlertDao;
	@Autowired
	private StoreConsumeBuyProductDao storeConsumeBuyProductDao;
	@Autowired
	private StoreProductDao storeProductDao;
	
	
	public StoreInventoryAlert get(Long id) {
		return storeInventoryAlertDao.findOne(id);
	}
	
	public List<StoreInventoryAlert> find(Page<StoreInventoryAlert> page, StoreInventoryAlert storeInventoryAlert) {
		DetachedCriteria dc = storeInventoryAlertDao.createDetachedCriteria();
		if (storeInventoryAlert.getStore()!=null && storeInventoryAlert.getStore().getId()>0){
			dc.add(Restrictions.eq("store.id", storeInventoryAlert.getStore().getId()));
		}
		dc.addOrder(Order.desc("id"));
		return storeInventoryAlertDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public StoreInventoryAlert isAlertExist(StoreProduct storeProduct) {
		Store store = StoreUtils.getStore(true);
		DetachedCriteria dc = storeInventoryAlertDao.createDetachedCriteria();
		if (store!=null && store.getId()>0){
			dc.add(Restrictions.eq("store.id", store.getId()));
		}
		if (storeProduct!=null && storeProduct.getId()>0){
			dc.add(Restrictions.eq("storeProduct.id", storeProduct.getId()));
		}
		List<StoreInventoryAlert> list = storeInventoryAlertDao.find(dc);
		return list == null || list.size() == 0 ? null : list.get(0);
	}
	@Transactional(readOnly = false)
	public List<StoreInventoryAlert> findStoreProduct() {
		Store store = StoreUtils.getStore(true);
		DetachedCriteria dc = storeInventoryAlertDao.createDetachedCriteria();
		if (store!=null && store.getId()>0){
			dc.add(Restrictions.eq("store.id", store.getId()));
		}
		return storeInventoryAlertDao.find(dc);
	}
	
	@Transactional(readOnly = false)
	public List<StoreInventoryAlert> findNeedAlertStoreProduct() {
		Store store = StoreUtils.getStore(true);
		DetachedCriteria dc = storeInventoryAlertDao.createDetachedCriteria();
		if (store!=null && store.getId()>0){
			dc.add(Restrictions.eq("store.id", store.getId()));
		}
		List<StoreInventoryAlert> needAlert = Lists.newArrayList();
		List<StoreInventoryAlert> list = storeInventoryAlertDao.find(dc);
		for (StoreInventoryAlert storeInventoryAlert : list) {
			if (storeInventoryAlert.getStoreProduct().getAmount() <= storeInventoryAlert.getAlertAmount()) {
				needAlert.add(storeInventoryAlert);
			}
		}
		return needAlert;
	}
	@Transactional(readOnly = false)
	public int  getProductAmount(Long pid) {
		List<StoreConsumeBuyProduct> list = storeConsumeBuyProductDao.findListByProduct(pid);
		int size = 0;
		Long end = DateUtils.getEndOfDay(new Date()).getTime();
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.DATE, -7);
		Long start = cal.getTimeInMillis();
		for (StoreConsumeBuyProduct storeConsumeBuyProduct : list) {
			StoreConsume storeConsume = storeConsumeBuyProduct.getStoreConsume();
			if (storeConsume != null && storeConsume.getConfirmed() == StoreConsume.TYPE_CONFIRMED && storeConsume.getConsume_Time().getTime() > start && storeConsume.getConsume_Time().getTime() < end) {
				size += storeConsumeBuyProduct.getAmount();
			}
		}
		return size;
	}
	@Transactional(readOnly = false)
	public void save(StoreInventoryAlert storeInventoryAlert) {
		storeInventoryAlertDao.save(storeInventoryAlert);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeInventoryAlertDao.delete(id);
	}
	
}
